package cn.sxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sxt.sys.constast.SYS_Constast;
import cn.sxt.sys.domain.Permission;
import cn.sxt.sys.domain.User;
import cn.sxt.sys.service.UserService;
import cn.sxt.sys.utils.DataGridView;
import cn.sxt.sys.utils.Md5Uitls;
import cn.sxt.sys.utils.PinyinUtils;
import cn.sxt.sys.utils.TreeNode;
import cn.sxt.sys.vo.PermissionVo;
import cn.sxt.sys.vo.RoleVo;
import cn.sxt.sys.vo.UserVo;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;

	// 跳到userManager.jsp
	@RequestMapping("toUserManager")
	public String toUserManager() {
		return "system/user/userManager";
	}

	// userManager.jsp加载 左边
	@RequestMapping("toUserLeft")
	public String toUserLeft() {
		return "system/user/userLeft";
	}

	// userManager.jsp加载 右边
	@RequestMapping("toUserRight")
	public String toUserRight() {
		return "system/user/userRight";
	}

	// 用户数据，分页，模糊
	@RequestMapping("queryAllUser")
	@ResponseBody
	public DataGridView queryAllUser(UserVo userVo) {
		return userService.queryAllUser(userVo);
	}

	// 跳添加页面
	@RequestMapping("toAddUser")
	public String toAddUser() {
		return "system/user/userAdd";
	}

	// 根据部门id查用户信息
	@RequestMapping("loadUserByDeptId")
	@ResponseBody
	public List<User> loadUserByDeptId(UserVo userVo) {
		return userService.queryUserByDeptId(userVo.getDeptid());
	}

	// 根据用户名生成拼音
	@RequestMapping(path = "initUserLoginNameUseUserName", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String initUserLoginNameUseUserName(String name) {
		String loginname = PinyinUtils.getPinYin(name);
		return loginname;
	}

	// 添加
	@RequestMapping("addUser")
	@ResponseBody
	public Map<String, Object> addUser(UserVo userVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			// 设置密码
			userVo.setPwd(
					Md5Uitls.encodePwdUseMd5(SYS_Constast.USER_PWD_DEFAULT, userVo.getName() + userVo.getAddress(), 2));
			// 设置类型
			userVo.setType(SYS_Constast.USER_TYPE_NORMAL);
			// 设置头像
			userVo.setImgpath(SYS_Constast.USER_DEFAULT_IMG);
			int i = userService.addUser(userVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateUser")
	public String toUpdateUser(UserVo userVo, Model model) {
		User user = userService.queryUserById(userVo.getId());
		model.addAttribute("user", user);
		return "system/user/userUpdate";
	}

	// 根据用户id查用户
	@RequestMapping("queryUserById")
	@ResponseBody
	public User queryUserById(UserVo userVo) {
		return userService.queryUserById(userVo.getId());
	}

	// 修改
	@RequestMapping("updateUser")
	@ResponseBody
	public Map<String, Object> updateUser(UserVo userVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = userService.updateUser(userVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 重置密码
	@RequestMapping("resetUserPwd")
	@ResponseBody
	public Map<String, Object> resetUserPwd(UserVo userVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = userService.resetUserPwd(userVo);
			if (i > 0) {
				msg = "重置成功";
			} else {
				msg = "重置失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "重置失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除
	@RequestMapping("deleteUser")
	@ResponseBody
	public Map<String, Object> deleteUser(UserVo userVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = userService.deleteUserById(userVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除日志
	@RequestMapping("batchDeleteUser")
	@ResponseBody
	public Map<String, Object> batchDeleteUser(UserVo userVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = userVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					userVo.setId(ids[i]);
					int j = userService.deleteUserById(userVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳分配角色页面
	@RequestMapping("toSelectUserRole")
	public String toSelectUserRole(UserVo userVo) {
		return "system/user/selectRole";
	}

	// 加载可用角色，选中用户已有的角色
	@RequestMapping("loadUserRole")
	@ResponseBody
	public DataGridView loadUserRole(UserVo userVo) {
		return userService.queryUserRole(userVo);
	}

	// 保存角色和权限菜单之间的关系
	@RequestMapping("saveUserRole")
	@ResponseBody
	public Map<String, Object> saveUserRole(UserVo userVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = userService.saveUserRole(userVo);
			if (i > 0) {
				msg = "分配成功";
			} else {
				msg = "分配失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "分配失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳改头像页面
	@RequestMapping("toupdateUserImg")
	public String toupdateUserImg() {
		return "system/user/userImg";
	}

	// 跳改密码页面
	@RequestMapping("toUpdatePwd")
	public String toUpdatePwd() {
		return "system/user/userUpdatePwd";
	}

	// 修改密码
	@RequestMapping("updatePwd")
	@ResponseBody
	public Map<String, Object> updatePwd(UserVo userVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = userService.updatePwd(userVo);
			if (i > 0) {
				msg = "密码修改成功";
			} else {
				msg = "密码修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "密码修改失败";
		}
		map.put("msg", msg);
		return map;
	}

}
